package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Constant;
import com.example.demo.model.entity.ScoreModel;
import com.example.demo.model.response.ScoreResponse;
import com.example.demo.model.response.UserResponse;
import com.example.demo.service.impl.ScoreService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = Constant.API_VERSION + "/score")
public class ScoreController {

    @Resource
    ScoreService scoreService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getScorePage(@RequestParam(value = "page_size", defaultValue = "10") String pageSize, @RequestParam(value = "page_number", defaultValue = "1") String pageNumber,
                                   @RequestParam(value = "user_id", defaultValue = "") final String userId, @RequestParam(value = "score_type", defaultValue = "") final String scoreType) throws Exception {

        Page<ScoreModel> scorePage = scoreService.getScorePage(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (StringUtils.isNotEmpty(userId)) {
                    Path<String> userIdPath = root.get("userId");
                    predicateList.add(criteriaBuilder.equal(userIdPath, userId));
                }
                if (StringUtils.isNotEmpty(scoreType)) {
                    Path<Integer> scoreTypePath = root.get("scoreType");
                    if (Integer.valueOf(scoreType) == Constant.SCORE_GET) {
                        predicateList.add(criteriaBuilder.greaterThan(scoreTypePath, 0));
                    } else {
                        predicateList.add(criteriaBuilder.lt(scoreTypePath, 0));
                    }
                }
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        }, new PageRequest(Integer.valueOf(pageNumber) - 1, Integer.valueOf(pageSize)));

        List<ScoreResponse> scoreResList = new ArrayList<>();
        for (ScoreModel s : scorePage.getContent()) {
            ScoreResponse res = new ScoreResponse();
            BeanUtils.copyProperties(res, s);
            UserResponse userRes = new UserResponse();
            BeanUtils.copyProperties(userRes, s.getUserModel());
            res.setUser(userRes);
            scoreResList.add(res);
        }


        JSONObject result = new JSONObject();
        result.put("total", scorePage.getTotalElements());
        result.put("items", scoreResList);
        return result;
    }

}
